package datamodel;

import org.jdom.Element;
import org.jdom.Namespace;

import java.util.LinkedList;

/**
 * Created by IntelliJ IDEA.
 * User: Qiang
 * Date: 24.11.2008
 * Time: 12:13:32
 */
public class MetaInfo implements IXmlWriteable {

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // constructor metode block
    public MetaInfo() {
        // default contructor
    }

    public MetaInfo(String ptag, String ptitle, String psourceType, String pyear,
                    String pcity, String ppublisher, String psourceURL, LinkedList<Author> pauthorlist) {
        this.tag = ptag;
        this.title = ptitle;
        this.sourceType = psourceType;
        this.year = pyear;
        this.city = pcity;
        this.publisher = ppublisher;
        this.sourceURL = psourceURL;
        this.authorList = pauthorlist;
    }

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // Interface Methode
    public void setFieldData(int i, String value) {
         // the sparql order ?Title ?Year ?City ?Publisher ?SourceType ?sourceURL
        switch (i) {
            case 0: this.title = value; break;
            case 1: this.year = value; break;
            case 2: this.city = value; break;
            case 3: this.publisher = value; break;
            case 4: this.sourceType = value; break;
            case 5: this.sourceURL = value; break;
            case 6: this.tag = value; break;
        }
    }

    public Element toXMLElement(Namespace ns) {
        Element sourceElement = new Element("Source", ns);
        sourceElement.addContent(new Element("Tag", ns).setText(this.tag));
        sourceElement.addContent(new Element("sourceURL").setText(this.sourceURL));
        sourceElement.addContent(new Element("Title", ns).setText(this.title));
        sourceElement.addContent(new Element("City", ns).setText(this.city));
        sourceElement.addContent(new Element("Year", ns).setText(this.year));
        sourceElement.addContent(new Element("Publisher", ns).setText(this.publisher));
        sourceElement.addContent(new Element("SourceType", ns).setText(this.sourceType));
        sourceElement.addContent(authorListToXMLElement(ns));
        return sourceElement;
    }

    public Element toXMLElement() {
        Element sourceElement = new Element("Source");
        sourceElement.addContent(new Element("Tag").setText(this.tag));
        sourceElement.addContent(new Element("sourceURL").setText(this.sourceURL));
        sourceElement.addContent(new Element("Title").setText(this.title));
        sourceElement.addContent(new Element("City").setText(this.city));
        sourceElement.addContent(new Element("Year").setText(this.year));
        sourceElement.addContent(new Element("Publisher").setText(this.publisher));
        sourceElement.addContent(new Element("SourceType").setText(this.sourceType));
        sourceElement.addContent(authorListToXMLElement());
        return sourceElement;
    }

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // other methods
    public Element authorListToXMLElement(Namespace ns) {
        Element nameListElement = new Element("NameList", ns);
        for (Author author : this.authorList) {
            Element authorEl = author.toXMLElement(ns);
            nameListElement.addContent(authorEl);
        }
        Element authorListPack = new Element("Author", ns)
                .addContent(new Element("Author", ns).addContent(nameListElement));
        return authorListPack;
    }

    public Element authorListToXMLElement() {
        Element nameListElement = new Element("NameList");
        for (Author author : this.authorList) {
            Element authorEl = author.toXMLElement();
            nameListElement.addContent(authorEl);
        }
        Element authorListPack = new Element("Author")
                .addContent(new Element("Author").addContent(nameListElement));
        return authorListPack;
    }

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // property block
    private String tag;
    private String title;
    private String sourceType;
    private String year;
    private String city;
    private String publisher;
    private String sourceURL;
    private LinkedList<Author> authorList = new LinkedList<Author>();

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // Getter and Setter block
    public String getTag() {
        return tag;
    }

    public void setTag(String tag) {
        this.tag = tag;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getSourceType() {
        return sourceType;
    }

    public void setSourceType(String sourceType) {
        this.sourceType = sourceType;
    }

    public String getYear() {
        return year;
    }

    public void setYear(String year) {
        this.year = year;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getPublisher() {
        return publisher;
    }

    public void setPublisher(String publisher) {
        this.publisher = publisher;
    }

    public String getSourceURL() {
        return sourceURL;
    }

    public void setSourceURL(String sourceURL) {
        this.sourceURL = sourceURL;
    }

    public LinkedList<Author> getAuthorList() {
        return authorList;
    }

    public void setAuthorList(LinkedList<Author> authorList) {
        this.authorList = authorList;
    }

    public String toString() {
        String reuslt = "[MetaInfo@" + this.sourceURL +"]";
        return reuslt;
    }

}